public class LoanCalculator {

    // 等额本金还款法的总利息
    public static double calcPrinInt(double loan, int months, double rate) {
        double monthlyRate = rate / 12;
        double monthlyPrincipal = loan / months;
        double totalInterest = (monthlyPrincipal * months + loan) / 2 * monthlyRate;
        return totalInterest;
    }

    // 等额本息还款法的总利息
    public static double calcEqInstInt(double loan, int months, double rate) {
        double monthlyRate = rate / 12;
        double monthlyPayment = loan * (monthlyRate / (1 - Math.pow(1 + monthlyRate, -months)));
        double totalInterest = monthlyPayment * months - loan;
        return totalInterest;
    }


}